FROM jupyter/datascience-notebook:hub-1.5.0
# https://hub.docker.com/r/jupyter/datascience-notebook/

WORKDIR /
USER root
# set local source.list
RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak
COPY ./linux_source/sources_ubuntu20.list /etc/apt/sources.list

# rstudio server / shiny ------------------ 
# https://github.com/rocker-org/rocker-versioned2
# https://www.docker.com/blog/user-guided-caching-in-docker-for-mac/
COPY scripts /rocker_scripts
# RUN awk '/^DEFAULT_USER=/ { $0 = "#" $0 }; 1' /rocker_scripts/install_rstudio.sh > /rocker_scripts/install_rstudio.sh 
# install_shiny_server: # install2.r  # cp -R $R_LIB/shiny/examples/* /srv/shiny-server/

ENV R_HOME=/opt/conda/lib/R
ENV R_LIB=/opt/conda/lib/R/library
ENV CRAN=https://packagemanager.rstudio.com/all/__linux__/focal/latest
ARG CRAN_URL=https://mirrors.tuna.tsinghua.edu.cn/CRAN

ENV S6_VERSION=v2.1.0.2
ENV RSTUDIO_VERSION=2023.03.0+386
ENV PANDOC_VERSION=default
ENV DEFAULT_USER=${NB_USER}
ENV PATH=/usr/lib/rstudio-server/bin:$PATH

RUN /rocker_scripts/install_rstudio.sh && \
    /rocker_scripts/install_pandoc.sh && \
    /rocker_scripts/install_shiny_server.sh && \
    ## folder permission
    chown -R $NB_UID:$NB_UID /srv/shiny-server/ && \
    chown -R $NB_UID:$NB_UID /var/lib/shiny-server && \
    chown -R $NB_UID:$NB_UID /var/log/shiny-server && \
    fix-permissions "${CONDA_DIR}" && \
    fix-permissions "/home/${NB_USER}"

# pkgs ------------------
# py pgks
# python3 -m pip install --upgrade https://github.com/jupyterhub/jupyter-rsession-proxy/tarball/master &&\
ENV RSESSION_PROXY_RSTUDIO_1_4=yes
RUN conda install --quiet --yes pip && \    
    python3 -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --no-cache \
        xgboost \
        sklearn2pmml \
        jupyter-rsession-proxy \
        jupyter-shiny-proxy && \ 
    rm -rf /tmp/* && \
    ## conda install
    conda install --quiet --yes 'JayDeBeApi' 'openpyxl' && \
    conda clean --all -f -y && \
    fix-permissions "${CONDA_DIR}" && \
    fix-permissions "/home/${NB_USER}"

# r pkgs 
RUN apt-get update && \
    apt-get install -y qpdf cron default-jdk && \
    apt-get clean && rm -rf /var/lib/apt/lists/* 

## r-dbi https://github.com/r-dbi # service cron start
RUN conda install --quiet --yes \
        'r-png' 'r-reticulate' 'r-pkgdown' \ 
        'r-odbc' 'r-rjdbc' 'r-rpostgres' 'r-RMySQL' && \
    conda clean --all -f -y && \
    fix-permissions "${R_HOME}" && \
    fix-permissions "/home/${NB_USER}"

    ## install.packages r pkgs
RUN R --quiet -e "install.packages(c('RClickhouse', 'blastula', 'rjson'), repos='${CRAN_URL}'); \
        devtools::install_github('bnosac/cronR')"  && \ 
    R --quiet -e "install.packages(c('pkgdown', 'blogdown', 'showtext'), repos='${CRAN_URL}')"  && \ 
    rm -rf /tmp/* && \
    fix-permissions "${R_HOME}" && \
    fix-permissions "/home/${NB_USER}"

## ml 
RUN R --quiet -e "install.packages(c( \
        'xgboost', 'mlr3', 'scorecard', 'xefun', \
        'tidytext', 'stringdist', 'jiebaR', \
        'tsibble', 'fable', 'pedquant'), repos='${CRAN_URL}')"  && \ 
    R --quiet -e "install.packages(c('h2o'), repos='${CRAN_URL}')"  && \ 
    rm -rf /tmp/* && \
    fix-permissions "${R_HOME}" && \
    fix-permissions "/home/${NB_USER}"

## shiny 
RUN R --quiet -e "install.packages(c( \
        'flexdashboard', 'bs4Dash', 'shinydashboard', 'shinydashboardPlus', \
        'shinyFeedback', 'shinyWidgets', \
        'bslib', 'sass', 'thematic', 'htmltools', 'jquerylib', 'htmlwidgets', \
        'plotly', 'leaflet', 'leaflet.minicharts', 'visNetwork', 'qgraph', \
        'shinythemes', 'shinycssloaders', 'shinyBS', 'shiny.i18n', 'shinyFiles', 'miniUI', \
         'sparkline', 'incidence', 'EpiEstim'), repos='${CRAN_URL}')"  && \ 
    rm -rf /tmp/* && \
    fix-permissions "${R_HOME}" && \
    fix-permissions "/home/${NB_USER}" 


# setting jupyter_shiny_proxy home directory to /srv/shiny-server
RUN cd /opt/conda/lib/python3.9/site-packages/jupyter_shiny_proxy &&  \
    awk '{sub(/os.getcwd\(\)/,"\"/srv/shiny-server\"")}1' __init__.py > /tmp/temp.py && mv /tmp/temp.py __init__.py && \ 
    cd /
# setting for service cron start
RUN chmod gu+rw /var/run/ && \
    chmod gu+s  /usr/sbin/cron

# startup ------------------
# R startup # https://rviews.rstudio.com/2017/04/19/r-for-enterprise-understanding-r-s-startup/
RUN echo 'options(repos = c(CRAN = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/", CRANextra = "https://cran.rstudio.com/"))' >> /opt/conda/lib/R/etc/Rprofile.site && \
    echo 'RETICULATE_PYTHON = "/opt/conda/bin/python"' >> /opt/conda/lib/R/etc/Renviron.site
# R_HOME, R_LIBS, LD_LIBRARY_PATH, PATH

# rstudio server config
RUN echo 'session-default-working-dir=/home/jovyan/work' >> /etc/rstudio/rsession.conf && \
    echo 'session-default-new-project-dir=/home/jovyan/work' >> /etc/rstudio/rsession.conf
COPY ./config/rstudio-prefs.json /etc/rstudio/rstudio-prefs.json

RUN mkdir -p /home/jovyan/share && \
    ln -s /home/jovyan/share /home/jovyan/work/share && \
    fix-permissions "/home/${NB_USER}"

# set localtime
RUN ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

WORKDIR $HOME
USER $NB_UID


# docker build --platform linux/amd64 -t shichenxie/dstudio_lab:1.5 -f ./Dockerfile_labds_jupyter1.5 .

# fix openssl issue # https://github.com/tschaffter/rstudio/issues/1
# RUN conda remove --force -y openssl
